// ActionScript file
import com.util.myLoader;

import flash.events.Event;
import flash.net.URLRequest;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;

public var bDebug:Boolean = false;


public function onCreate():void
{
	//System.useCodePage = true;
	//onSearch();   
}

//public var q:String;
public var list_type:String = 'popular';
public var term_type:String = 'month';

public var searchSite:String = ''; // svn test  --> tag test 



public function initModule():void {
	iSaveCnt = 0;	
	arSaveData = [];
} 


//@@@@@@@@@@@@@@@@@@@  1004tv List @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public var code1004:String;
public function onSearch_1004tv(code:String):void
{
	initModule();
	code1004	= code;
	searchSite = '1004tv';
	System.useCodePage = true;	
	var s1004tv_Url:String = "http://1004tv.kr/torrent/bbs/board.php?bo_table="+code;

	taLog.text += ' 검색 시작(1004tv).. \r';			
	var vUrlLoader:myLoader;
	vUrlLoader = new myLoader();
	vUrlLoader.sSearch = '';
	vUrlLoader.addEventListener(Event.COMPLETE, onResultView_1004tv);
	vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
	vUrlLoader.load(new URLRequest(s1004tv_Url));		
}
public function onResultView_1004tv(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
		
	var listStr:String = new String(e.currentTarget.data);
	var ptList:RegExp = /<!-- 글제목 -->.+?<\/tr>/gs;
	//var ptDetail:RegExp = /<a href="(?P<url>.+?)".+?title="(?P<title>.+?)"/s;	
	var ptDetail:RegExp = /bo_table=(?P<url>.+?)".+?<span>(?P<title>.+?)<\/span>/s;
	
	var ag:Array = listStr.match(ptList);
	var sParsing:String;
	var objGet:Object;
	var iTotCnt:int = 15;
	
	for(var i:int=0; i<iTotCnt; i++) {
		sParsing = ag[i].toString();
		objGet = ptDetail.exec(sParsing);  
		if(objGet) {			
			sUrl = "http://1004tv.kr/torrent/bbs/board.php?bo_table="+objGet.url;			
			sTitle = objGet.title;
		
			
			sTitle = sTitle.replace(/&nbsp;/g, " ");
			
			sUser = "no" ;

		 	var objParam:Object = {};
			objParam.sId = sUser;				// sId	
			objParam.ag = 'brdNum'; 			// brdNum
			objParam.suburl = sUrl; 
			objParam.sWhere = '1004tv';
			objParam.url = sUrl;    
			objParam.sSearch = '';
			
			objParam.sTitle = sTitle;
			objParam.sSearch = '';
			
			isHaveData(objParam, '1004tv', iTotCnt); 			
		}
	}
}
public function onResultViewDetail_1004tv(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
	var ptList:RegExp = /<div id=view(.+?)<!-- 테러 태그 방지용 -->/gs;	
	var listStr:String = new String(e.currentTarget.data);	
	
	// 동영상만 뽑아내야함..	
	var ag:Array = listStr.match(ptList);
	if(ag.length == 0) {
		iSaveCnt++;
		trace("onResultViewDetail_1004tv : contetn 추출불가");
		return;
	}
	
	var sContent = ag[0].toString() + '</div>';
	var sContent = sContent.replace("zang", "wghost");
	 
	var sendObj:Object 	= e.currentTarget.sendObj;
	var sId:String 		= sendObj.sId;
	var sSubUrl:String 	= sendObj.suburl;
	var sUrl:String 	= sendObj.suburl;
	var sTitle:String 	= sendObj.sTitle + ' 다시보기';
	var totCnt:int 	= sendObj.totCnt;
						
	var sSearch:String 	= e.target.sSearch;	
	
	sendObj.content = sContent;
	sendObj.sTitle = sendObj.sTitle + ' 다시보기';
	
	
	
	
	
	
	// 배열로 담아 수집할거라면..
	if(totCnt > 0) {
 		iSaveCnt++;
 		trace('totCnt : ' + totCnt + ', iSaveCnt : ' + iSaveCnt);
		arSaveData.push(sendObj);
		// 총 수집예상양과 수집된 양과 같다면 담아놓은 배열을 세부 글 내용 파싱에 사용할 함수를 호출
		if(totCnt == iSaveCnt) {			
			dbSaveArr(arSaveData)
		}				
	}
	// 기존로직이라면
	else {
		dbSave(sTitle, sContent, sId, sSubUrl, '', '', '', '', '1004tv');	
	}		
	
}










//@@@@@@@@@@@@@@@@@@@  jjang List @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onSearch_jjang():void
{
	initModule();
	searchSite = 'jjang';
		
	var steel_Url:String = "http://www.jjang0u.com/Chalkadak/main.html?page=";
	System.useCodePage = true;
	for(var i:int=1; i<6; i++) {
		taLog.text += ' 검색 시작(jjang0u).. \r';			
		var vUrlLoader:myLoader;
		vUrlLoader = new myLoader();
		vUrlLoader.sSearch = '';
		vUrlLoader.addEventListener(Event.COMPLETE, onResultView_jjang);
		vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
		vUrlLoader.load(new URLRequest(steel_Url+i));		
	}	
}
public function onResultView_jjang(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
		
	var listStr:String = new String(e.currentTarget.data);
	var ptList:RegExp = /board_img_list_b">.+?<\/a>/gs;
	var ptDetail:RegExp = /board_img_list_b">.+?<a href.+?><img src="(?P<img>.+?)".+? alt="(?P<title>.+?)"><\/a>/s;	
	
	
	var ag:Array = listStr.match(ptList);
	var sParsing:String;
	var objGet:Object;
	
	for(var i:int=0; i<ag.length; i++) {					
		sParsing = ag[i].toString();
		objGet = ptDetail.exec(sParsing);  
		if(objGet) {			
			sUrl 	= objGet.img;			
			sTitle 	= objGet.title;
		
			
			sTitle = sTitle.replace(/&nbsp;/g, " ");
			
			sUser = "no" ;

		 	var objParam:Object = {};
			objParam.sId = sUser;				// sId	
			objParam.ag = 'brdNum'; 			// brdNum
			objParam.suburl = sUrl; 
			objParam.sWhere = 'jjang';
			objParam.url = sUrl;    
			objParam.sSearch = '';
			
			objParam.sTitle = sTitle;
			objParam.sSearch = '';
			
			isHaveData(objParam, 'jjang'); 			
		}
	}
}
public function onResultViewDetail_jjang(obj:Object):void
{
	var sendObj:Object 	= obj;	
	var sContent = "";
	sContent = String(sendObj.suburl).replace(/t_/g, "");
	sContent = "<img src='"+sContent+"' />"		
	var sId:String 		= sendObj.sId;
	var sSubUrl:String 	= sendObj.suburl;
	var sUrl:String 	= sendObj.suburl;
	
	var sTitle:String 	= sendObj.sTitle;
	var sSearch:String 	= 'no';	
		
	dbSave(sTitle, sContent, sId, sSubUrl, '', '', '', '', 'jjang');
}






//@@@@@@@@@@@@@@@@@@@  oldtale List @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onSearch_oldtale():void
{
	initModule();
	searchSite = 'oldtale';
		
	var oldtale_Url:String = "http://oldtale.tistory.com/m/post/list/page/";
	

	for(var i:int=1; i<10; i++) {
		taLog.text += ' 검색 시작(oldtale).. \r';			
		var vUrlLoader:myLoader;
		vUrlLoader = new myLoader();
		vUrlLoader.sSearch = '';
		vUrlLoader.addEventListener(Event.COMPLETE, onResultView_oldtale);
		vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
		vUrlLoader.load(new URLRequest(oldtale_Url+i));		
	}	
}
public function onResultView_oldtale(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
		
	var listStr:String = new String(e.currentTarget.data);
	var ptList:RegExp = /<li.+?<\/li>/gs;
	//var ptDetail:RegExp = /<a href="(?P<url>.+?)".+?title="(?P<title>.+?)"/s;	
	var ptDetail:RegExp = /<a.+?title="(?P<title>.+?)".+href="(?P<url>.+?)">/s;
	
	
	var ag:Array = listStr.match(ptList);
	var sParsing:String;
	var objGet:Object;
	for(var i:int=0; i<ag.length; i++) {					
		sParsing = ag[i].toString();
		objGet = ptDetail.exec(sParsing);  
		if(objGet) {			
			sUrl = "http://oldtale.tistory.com"+objGet.url;			
			sTitle = objGet.title;
		
			
			sTitle = sTitle.replace(/&nbsp;/g, " ");
			
			sUser = "no" ;

		 	var objParam:Object = {};
			objParam.sId = sUser;				// sId	
			objParam.ag = 'brdNum'; 			// brdNum
			objParam.suburl = sUrl; 
			objParam.sWhere = 'oldtale';
			objParam.url = sUrl;    
			objParam.sSearch = '';
			
			objParam.sTitle = sTitle;
			objParam.sSearch = '';
			
			isHaveData(objParam, 'oldtale'); 			
		}
	}
}
public function onResultViewDetail_oldtale(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
	var ptList:RegExp = /<!-- 글 내용시작 -->(.+?)<span class="area_comment">/gs;	
	var listStr:String = new String(e.currentTarget.data);	
	
	// 동영상만 뽑아내야함..	
	var ag:Array = listStr.match(ptList);
	
	
	var sContent = ag[0].toString(); 
	var sendObj:Object 	= e.currentTarget.sendObj;
	var sId:String 		= sendObj.sId;
	var sSubUrl:String 	= sendObj.suburl;
	var sUrl:String 	= sendObj.suburl;
	var sTitle:String 	= sendObj.sTitle;
	var sSearch:String 	= e.target.sSearch;	
		
	dbSave(sTitle, sContent, sId, sSubUrl, '', '', '', '', 'steel');
}


















//@@@@@@@@@@@@@@@@@@@  steel List @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onSearch_steel():void
{
	searchSite = 'steel';
		
	var steel_Url:String = "http://steelseries.tistory.com/m/post/list/page/";
	

	for(var i:int=1; i<10; i++) {
		taLog.text += ' 검색 시작(steel).. \r';			
		var vUrlLoader:myLoader;
		vUrlLoader = new myLoader();
		vUrlLoader.sSearch = '';
		vUrlLoader.addEventListener(Event.COMPLETE, onResultView_steel);
		vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
		vUrlLoader.load(new URLRequest(steel_Url+i));		
	}	
}
public function onResultView_steel(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
		
	var listStr:String = new String(e.currentTarget.data);
	var ptList:RegExp = /<li.+?<\/li>/gs;
	//var ptDetail:RegExp = /<a href="(?P<url>.+?)".+?title="(?P<title>.+?)"/s;	
	var ptDetail:RegExp = /<a.+?title="(?P<title>.+?)".+href="(?P<url>.+?)">/s;
	
	
	var ag:Array = listStr.match(ptList);
	var sParsing:String;
	var objGet:Object;
	for(var i:int=0; i<ag.length; i++) {					
		sParsing = ag[i].toString();
		objGet = ptDetail.exec(sParsing);  
		if(objGet) {			
			sUrl = "http://steelseries.tistory.com"+objGet.url;			
			sTitle = objGet.title;
		
			
			sTitle = sTitle.replace(/&nbsp;/g, " ");
			
			sUser = "no" ;

		 	var objParam:Object = {};
			objParam.sId = sUser;				// sId	
			objParam.ag = 'brdNum'; 			// brdNum
			objParam.suburl = sUrl; 
			objParam.sWhere = 'steel';
			objParam.url = sUrl;    
			objParam.sSearch = '';
			
			objParam.sTitle = sTitle;
			objParam.sSearch = '';
			
			isHaveData(objParam, 'steel'); 			
		}
	}
}
public function onResultViewDetail_steel(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
	var ptList:RegExp = /<!-- 글 내용시작 -->(.+?)<span class="area_comment">/gs;	
	var listStr:String = new String(e.currentTarget.data);	
	
	var ag:Array = listStr.match(ptList);
	var sContent = ag[0].toString(); 
	var sendObj:Object 	= e.currentTarget.sendObj;
	var sId:String 		= sendObj.sId;
	var sSubUrl:String 	= sendObj.suburl;
	var sUrl:String 	= sendObj.suburl;
	var sTitle:String 	= sendObj.sTitle;
	var sSearch:String 	= e.target.sSearch;	
		
	dbSave(sTitle, sContent, sId, sSubUrl, '', '', '', '', 'steel');
}










//@@@@@@@@@@@@@@@@@@@  ygosu List @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onSearch_Ygosu():void
{
	initModule();
	searchSite = 'ygosu';


		
	var ygosu_Url:String = "http://m.ygosu.com/board/best_article/$page=";
	

	for(var i:int=1; i<5; i++) {
		taLog.text += ' 검색 시작(ygosu).. \r';			
		var vUrlLoader:myLoader;
		vUrlLoader = new myLoader();
		vUrlLoader.sSearch = '';
		vUrlLoader.addEventListener(Event.COMPLETE, onResultView_ygosu);
		vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
		vUrlLoader.load(new URLRequest(ygosu_Url+i));		
	}	

}

public function onResultView_ygosu(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
		
	var listStr:String = new String(e.currentTarget.data);
	var ptList:RegExp = /<li>.+?<\/li>/gs;
	var ptDetail:RegExp = /<a href='(?P<url>.+?)'><em>(?P<title>.+?)<span>.+?<p>(?P<user>.+?)<span>/s;	
	var ag:Array = listStr.match(ptList);
	var sParsing:String;
	var objGet:Object;
	for(var i:int=0; i<ag.length; i++) {					
		sParsing = ag[i].toString();
		objGet = ptDetail.exec(sParsing);  
		if(objGet) {			
			sUrl = "http://m.ygosu.com"+objGet.url;			
			sTitle = objGet.title;
			
			if(sTitle.indexOf("와이고수") >= 0 || sTitle.indexOf("와고") >= 0 || sTitle.indexOf("미네랄") >= 0 ) {
				continue;
			}
			
			sTitle = sTitle.replace(/&nbsp;/g, " ");
			
			sUser = objGet.user;

		 	var objParam:Object = {};
			objParam.sId = sUser;				// sId	
			objParam.ag = 'brdNum'; 			// brdNum
			objParam.suburl = sUrl; 
			objParam.sWhere = 'ygosu';
			objParam.url = sUrl;    
			objParam.sSearch = '';
			
			objParam.sTitle = sTitle;
			objParam.sSearch = '';
			
			isHaveData(objParam, 'ygosu'); 			
		}
	}
}

//@@@@@@@@@@@@@@@@@@@  ygosu Data view @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onResultViewDetail_ygosu(e:Event):void
{
	var sUrl:String;
	var sTitle:String;
	var sUser:String;
	var ptList:RegExp = /<div class="contain">.+?<\/div>/gs;	
	var listStr:String = new String(e.currentTarget.data);	
	
	var ag:Array = listStr.match(ptList);
	var sContent = ag[0].toString(); 
	var sendObj:Object 	= e.currentTarget.sendObj;
	var sId:String 		= sendObj.sId;
	var sSubUrl:String 	= sendObj.suburl;
	var sUrl:String 	= sendObj.suburl;
	var sTitle:String 	= sendObj.sTitle;
	var sSearch:String 	= e.target.sSearch;	
		
	dbSave(sTitle, sContent, sId, sSubUrl, '', '', '', '', 'ygosu');
}








//@@@@@@@@@@@@@@@@@@@  duam @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public function onSearch(q:String):void
{
	searchSite = 'daum';
	
	taLog.text += q + '로 검색 시작(daum).. \r';
	if(q=="마이프린세스") {
		trace("dd");
	}
	
	System.useCodePage = true;
	//q = encodeURIComponent(tiSearch.text);
	var vUrlLoader:myLoader;
	vUrlLoader = new myLoader();
	vUrlLoader.sSearch = q;
	vUrlLoader.addEventListener(Event.COMPLETE, onResultView);
	vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
	vUrlLoader.load(new URLRequest(sDurl+'&q='+q));
}
public function onResultView(e:Event):void
{	
	var sSearch:String = e.target.sSearch;
	
	var listXml:XMLList = new XMLList(e.currentTarget.data);
	var entityAc:XMLList = listXml.entity.news;
	var sUrl:String;
	var sId:String;

	var iEntityAc:int = entityAc.length();
	var sItem:Object;	
	for(var i:int=0; i<iEntityAc; i++)
	{
		//if(i==3) return;
		
		sItem = entityAc[i];
		sUrl = sItem.url.toString();
		sId = sItem.id.toString();
		var objParam:Object = {};
		if(sUrl.indexOf(tstUrl) >=0)
		{
			if(sUrl.indexOf('entry') >= 0 )
			{
				// 추후 entry쪽 링크 타고 들어가기.. 
				continue;
			}
			else 
			{
				var regId:RegExp = /http:\/\/(.+?).tistory.com\/(.+)/;
				var agId:Array = sUrl.match(regId);
				var sSubUrl:String = 'http://' + agId[1] + '.' + tstUrl + '/sync/'+agId[2];
				//trace('sSubUrl : ' + sSubUrl);
				objParam.sId = sId;
				objParam.ag = agId[2]; 
				objParam.suburl = sSubUrl; 
				objParam.sWhere = 'tst';
				objParam.url = sUrl;    
				objParam.sSearch = sSearch;
				isHaveData(objParam);
				//isSaveData(sId, agId[2], sSubUrl, 'tst', sSearch, sUrl)				
			}
		}
		else if(sUrl.indexOf(dmblgUrl) >=0 ) {				
				var regId_blg:RegExp 	= /http:\/\/blog.daum.net\/(.+?)\/(.+)/;
				var agId_blg:Array 		= sUrl.match(regId_blg);
				//var sSubUrl_blg:String 	= dmblgUrl + '/' + agId_blg[1] + '/' + agId_blg[2];
				var sSubUrl_blg:String 	= "http://apis.daum.net/blog/ArticleView.do?blogname="+agId_blg[1]+'&articleno='+agId_blg[2]+'&apikey=babd701a3e83414d899bdc745f0de6bab781d670';
				objParam.sId = sId;
				objParam.ag = agId_blg[2]; 
				objParam.suburl = sSubUrl_blg; 
				objParam.sWhere = 'blg'; 
				objParam.url = sUrl;    	
				objParam.sSearch = sSearch;
				
				isHaveData(objParam);			
				//trace('sSubUrl_blg : ' + sSubUrl_blg);
				//isSaveData(sId, agId_blg[2], sSubUrl_blg, 'blg', sSearch, sUrl);			
		}
		
			
	}
}


/////////////////////////////////////////////////////////////////////////////
// naver
/////////////////////////////////////////////////////////////////////////////
public function onSearch_naver(q:String):void
{
	searchSite = 'naver';
	taLog.text += q + '로 검색 시작(naver).. \r';

//	System.useCodePage = false;
	//q = encodeURIComponent(tiSearch.text);
	var vUrlLoader:myLoader;
	vUrlLoader = new myLoader();
	vUrlLoader.sSearch = q;
	vUrlLoader.addEventListener(Event.COMPLETE, onResultView_naver);
	vUrlLoader.addEventListener(FaultEvent.FAULT, onFalutView);	
	vUrlLoader.load(new URLRequest(naverUrl+q));
}

public function onResultView_naver(e:Event):void
{	
	var listXml:XMLList = new XMLList(e.currentTarget.data);
	var sSearch:String = e.currentTarget.sSearch.toString();
	var entityAc:XMLList = listXml.channel.item;
	
	var iEntityAc:int = entityAc.length();
	var sItem:Object;
	var sUrl:String;
	var sId:String;
	var sTitle:String;
	for(var i:int=0; i<iEntityAc; i++) {
		 sItem = entityAc[i];
		 trace('sItem : ' +sItem);
		 sUrl 	= sItem.link.toString();
		 sId 	= sItem.bloggername.toString();
		 sTitle = sItem.title.toString();
		 goBlgLink(sUrl, sTitle, sSearch);
	}	
}

public function goBlgLink(sUrl:String, sTitle:String, sSearch:String):void {
	var vUrlLoaderSub_Tst:myLoader = new myLoader(); 	
	vUrlLoaderSub_Tst.addEventListener(FaultEvent.FAULT, onFalutView);
	vUrlLoaderSub_Tst.addEventListener(IOErrorEvent.IO_ERROR, ioErrorhandler);
		
	vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE,
		function(e:Event):void {onResultBlgData(e, sTitle, sSearch);}
	);
	vUrlLoaderSub_Tst.load(new URLRequest(sUrl) );
}

public function onResultBlgData(e:Event, sTitle:String, sSearch:String):void {
	var listString:String = new String(e.currentTarget.data);
	var regId:RegExp = /PostView.nhn\?blogId=(.+?)&logNo=(.+?)&/s;
	var agId:Array = listString.match(regId);
	if(agId == null) {
		return;
	}
	var sSubUrl:String = mBlgUrl+'/'+agId[1] + '/'+agId[2];
	
	var objParam:Object = {};
	objParam.sId = agId[1];				// sId	
	objParam.ag = 'brdNum'; 			// brdNum
	objParam.suburl = sSubUrl; 
	objParam.sWhere = 'naver';
	objParam.url = sSubUrl;    
	objParam.sSearch = sSearch;
	
	objParam.sTitle = sTitle;
	objParam.sSearch = sSearch;
	
	isHaveData(objParam, 'naver');
					
	//goBlgContent(sSubUrl, sTitle);
}

/////////////////////////////////////////////////////////////////////////////
// naver
/////////////////////////////////////////////////////////////////////////////



public function isHaveData(objParam:Object, dv:String='daum', totCnt:int=0):void 
{
	try {							
		var sUrl:String = objParam.url;
		var sUrlInsert:String = '';
		if(searchSite=='ygosu') {
			var ygosuUrlReg:RegExp = /(.+?)\$/g;
			var ag:Array = sUrl.match(ygosuUrlReg);
			sUrlInsert = ag[0].toString(); 				
		} 
		else {
			sUrlInsert = sUrl;
		}

		
	    var st:Statement = con.createStatement();	
		var SQL:String;				
		SQL =       "select url from g4_silver_data ";
		SQL = SQL + "where url = ?" ;
		st.setString(1, sUrlInsert);		
		trace(" isHaveData sUrl : " + sUrlInsert); 
		//SQL = 'select wr_link1 from g4_write_review_ent_08 union select wr_link1 from g4_write_review_ent_01 union select wr_link1 from g4_write_review_ent_02 union select wr_link1 from g4_write_review_ent_03 union select wr_link1 from g4_write_review_ent_04 union select wr_link1 from g4_write_review_ent_05 union select wr_link1 from g4_write_review_ent_06 union select wr_link1 from g4_write_review_ent_07 union select wr_link1 from g4_write_rv_drama_01 union select wr_link1 from g4_write_rv_drama_02 union select wr_link1 from g4_write_rv_drama_03 union select wr_link1 from g4_write_rv_drama_04 union select wr_link1 from g4_write_rv_drama_05 union select wr_link1 from g4_write_rv_drama_06 union select wr_link1 from g4_write_rv_drama_07 union select wr_link1 from g4_write_rv_drama_08 union select wr_link1 from g4_write_rv_drama_09 union select wr_link1 from g4_write_rv_drama_10 union select wr_link1 from g4_write_rv_game_01 union select wr_link1 from g4_write_rv_game_02 union select wr_link1 from g4_write_rv_game_03 union select wr_link1 from g4_write_rv_game_04 union select wr_link1 from g4_write_rv_game_05 union select wr_link1 from g4_write_rv_game_07 union select wr_link1 from g4_write_rv_game_08 union select wr_link1 from g4_write_rv_game_09 union select wr_link1 from g4_write_rv_game_10';
		st.getConnection();				
		
	    var token:MySqlToken = st.executeQuery(SQL);
	    objParam.dv = dv;	
	    objParam.totCnt = totCnt;
	    token.param = objParam;			        
	    token.addResponder(new AsyncResponder(isHaveDataHandler, onFault, token));
	}
	catch (e:Error) {
		getConnection(); 
	}
}

public var iSaveCnt:int = 0;
public var arSaveData:Array;
private function isHaveDataHandler(data:Object, tokenCount:Object):void
{
	var rs:ResultSet;
	rs = ResultSet(data)
	
	if ( rs != null) {
		
		var arc:ArrayCollection = rs.getRows();
		
		// 중복 url pk가 있을경우...						
		if(arc.length >= 1) {
			iSaveCnt++;
			trace(tokenCount.param.suburl+' 이미 있음 ............');			 					
			return;			
		}
		// 없을경우 pk 인서트 시키고 .. 실제 board 테이블에 인서트 
		else{			
			var sUrlInsert:String = '';
			if(searchSite=='ygosu') {
				var sUrl:String = tokenCount.param.url;
				var ygosuUrlReg:RegExp = /(.+?)\$/g;
				var ag:Array = sUrl.match(ygosuUrlReg);
				sUrlInsert = ag[0].toString(); 				
			} 
			else {
				sUrlInsert = tokenCount.param.url;
			}

			if(!bDebug) {
				urlKeyInsert(sUrlInsert);	
			}
	 		isSaveData(tokenCount.param);
		} 
	}					
}	

////////////////////////////c////////////////////////////
// pk url 입력 
private function urlKeyInsert(url:String):void
{
	try {
		var st:Statement = con.createStatement();
		st.sql = "INSERT INTO g4_silver_data (url) VALUES (?);"
		st.setString(1, url);		
												
		var token:MySqlToken = st.executeQuery();
		token.addResponder(new AsyncResponder(urlKeyInsertHandler, onFault, token));
	} catch (e:Error) {					
		trace("urlKeyInsert err : " + e.message.toString());
	} 				
}
private function urlKeyInsertHandler( data:Object, token:Object):void
{
	//trace("ok");
}

public function isSaveDataArr(arr:Array):void {
	
}

//public function isSaveData(sId:String, brdNum:String, sSubUrl:String, type:String, sSearch:String, sUrl:String, dv:String, sendObj:Object):void
public function isSaveData(sendObj:Object):void
{
	try
	{
		//return;
//isSaveData(	tokenCount.param.sId, tokenCount.param.ag, tokenCount.param.suburl, tokenCount.param.sWhere, 
//						tokenCount.param.sSearch, tokenCount.param.url, tokenCount.param.dv, tokenCount.param);
		
		trace("isSaveData@sSubUrl : " + sendObj.suburl);
		taLog.text += sendObj.suburl+'새로운글 등장..................\r';
		
		
		taLog.validateNow();
        taLog.verticalScrollPosition = taLog.maxVerticalScrollPosition;
            		
		
		var type:String = sendObj.sWhere;
		if(type =='jjang') {
			onResultViewDetail_jjang(sendObj);
			return;
		}	
					
		var vUrlLoaderSub_Tst:myLoader = new myLoader();
		vUrlLoaderSub_Tst.sSearch = sendObj.sSearch;
		vUrlLoaderSub_Tst.sendObj = sendObj; 
		
		vUrlLoaderSub_Tst.addEventListener(FaultEvent.FAULT, onFalutView);
		vUrlLoaderSub_Tst.addEventListener(IOErrorEvent.IO_ERROR, ioErrorhandler);
	
		System.useCodePage = false;
		if(type =='tst') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewSub_tst);
		}
		else if(type == 'blg') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewSub_blg);
		}
		else if(type == 'naver') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewSub_naver);
		}
		else if(type == 'ygosu') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewDetail_ygosu);
		}
		else if(type =='steel') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewDetail_steel);
		}		
		else if(type =='oldtale') {
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewDetail_oldtale);
		}
		else if(type =='1004tv') {
			System.useCodePage = true;		
			vUrlLoaderSub_Tst.addEventListener(Event.COMPLETE, onResultViewDetail_1004tv);
		}		
					
		vUrlLoaderSub_Tst.load(new URLRequest(sendObj.suburl));
	}
	catch (e:Error)
	{
	 trace(e.message);	
	}
}

public function ioErrorhandler(e:Event):void
{
	trace('ioErrorhandler:'+e.toString());
	taLog.text += e.toString();
}


// naver parsing 
public function onResultViewSub_naver(e:Event):void {

	var sendObj:Object = e.currentTarget.sendObj;
	var sUrl:String = sendObj.suburl;
	var sTitle:String = sendObj.sTitle;
	var sSearch:String 		= e.target.sSearch;	
	 
	
	trace('sUrl : ' + sUrl);
	trace('sTitle : ' + sTitle);
	
	var contentString:String = new String(e.currentTarget.data);
	var regId:RegExp = /<div class="rc">(.+?)<\/div>.+?<div class="sc6">/s;
	var agId:Array = contentString.match(regId);
	if(agId == null) {
		return;  
	}
	var sContent:String = agId[1];	
	
	dbSave(sTitle, sContent, sendObj.sId, sendObj.suburl, '', '', '', '', sendObj.sSearch);
}


// tistory용 파싱
public function onResultViewSub_tst(e:Event):void
{
	try {
		var sSearch:String 		= e.target.sSearch;
		var listXml:XMLList 	= new XMLList(e.currentTarget.data);
		var sTitle:String 		= listXml.entry.title;
		var sContent:String	 	= listXml.entry.content;
		var sAuthor:String	 	= listXml.entry.author;
		var pLink:String	 	= listXml.entry.permalink;
		
		var xlTag:XMLList		= listXml.entry.tag;
		var sTag:String			= '';
		
		var xlAttachment:XMLList	= listXml.entry.attachment;
		var arFileUrl:Array			= [];
		var arFileName:Array		= [];
		var arFileType:Array		= [];
		
		trace('sTitle : [' + pLink +'] ' + sTitle);
		
		for(var i:int=0; i<xlTag.length(); i++)
		{
			sTag += xlTag[i] + ',';	
		}
	
		for(var i:int=0; i<xlAttachment.length(); i++)
		{
			arFileUrl.push(String(xlAttachment[i].url).replace("image", "T150x150")  );
			arFileName.push(xlAttachment[i].filename);
			arFileType.push(xlAttachment[i].mimeType);
		}
		
		dbSave(sTitle, sContent, sAuthor, pLink, sTag, arFileUrl[0], arFileUrl[1], arFileUrl[2], sSearch);
	}
	catch(e:Error) {
		trace(e.message);
	}								
}
		
// 블로그용 파싱
public function onResultViewSub_blg(e:Event):void
{
	try {
		//return;
		var sSearch:String 		= e.target.sSearch;
		var listXml:XMLList 	= new XMLList(e.currentTarget.data);
		var sTitle:String 		= listXml.title;
		var sContent:String	 	= listXml.description;
		var sAuthor:String	 	= listXml.author;
		var xlTag:XMLList		= listXml.tag;
		var sTag:String			= '';
		var pLink:String	 	= listXml.link;
		
		var xlAttachment:XMLList	= listXml.image;
		var arFileUrl:Array			= [];
		var arFileName:Array			= [];
		var arFileType:Array			= [];
		
		sContent = sContent.replace(/&amp;nbsp;/g, "&nbsp;");     
		
		trace('sTitle : [' + pLink +'] ' + sTitle);
		for(var i:int=0; i<xlTag.length(); i++)
		{
			sTag += xlTag[i] + ',';	
		}
	
		for(var i:int=0; i<xlAttachment.length(); i++)
		{
			arFileUrl.push(xlAttachment[i].thumbnail_m);
			//arFileName.push(xlAttachment[i].filename);
			//arFileType.push(xlAttachment[i].mimeType);
		}	
		
		dbSave(sTitle, sContent, sAuthor, pLink, sTag, arFileUrl[0], arFileUrl[1], arFileUrl[2], sSearch);
	}
	catch(e:Error) {
		trace(e.message);
	}				
}			

public function dbSaveArr(arr:Array):void {
	trace(ObjectUtil.toString(arr) );
	var sItem:Object;;	
	for(var i:int; i<arr.length; i++) {
		sItem = arr[i];
		dbSave(sItem.sTitle, sItem.content, 'sAuthor', sItem.suburl, 'stag', 
		'fileurl0', 'fileurl1','fileurl2',sItem.sWhere);
	}
}



public var ptDeltag5:RegExp =  /<script.+?<\/script>|<iframe.+?<\/iframe>|<button.+?<\/button>|<form.+?<\/form>/gsi;
//public var ptDeltag2 =  /<script.+?<\/script>/gsi;

public function dbSave(	sTitle:String, sContent:String, sAuthor:String, pLink:String, sTag:String, 
						arFileUrl0:String, arFileUrl1:String, arFileUrl2:String, sSearch:String):void
{	
	trace('sTitle : ' + sTitle + ', pLink : ' + pLink);	
	sContent = sContent.replace(ptDeltag5, '');
//	sContent = sContent.replace(ptDeltag2, '');

	System.useCodePage = false;	
	if(sTitle == null || sTitle == '') {
		return;
	}	
	var i2:int = (Math.random()* 1000)%45;
	
	var obj:Object = new Object();	
	obj.mb_id = arTempMemberId[i2].toString();
	obj.wr_name = arTempMemberAlias[i2].toString();
	
		
	var objHm:Object;
	var sBo_table:String;
	if(sSearch=='ygosu') {
		sBo_table 	= 'joke';
	}
	else if(sSearch=='steel') {
		sBo_table 	= 'joke';
	}
	else if(sSearch=='jjang') {
		sBo_table 	= 'jjalbang';
	}
	else if(sSearch=='oldtale') {
		// air에서  mysql에 저장되어있는 키워드를 바탕으로 저장시킬 테이블 이름을 뽑아냄 ..
		//drama8
		//ent5
		//avi6
		sBo_table 	= '';
	}			
	else if(sSearch=='1004tv') {
		if(code1004=="B32") {
			sBo_table 	= 'drama8';
		} else if(code1004=="B33") {
			sBo_table 	= 'ent5';
		}
		obj.mb_id = 'admin';
		obj.wr_name = '탑킹';		
	}
	else {
		objHm 		= hm.getValue(sSearch);
		sBo_table 	= objHm.bo_table;		
	} 		
 	
	
	obj.wr_num = '1';
	obj.wr_reply = '1';
	obj.ca_name = '';
	obj.html = 'html1';
	obj.secret = '1';
	obj.mail = '2';
	obj.wr_subject = sTitle;
	obj.wr_content = sContent;
	obj.wr_link1 = pLink;
	obj.wr_link2 = '';
	obj.wr_trackback = '';
	obj.member = '1';
	obj.wr_password = '7180';

	obj.wr_email = '';
	obj.wr_homepage = '';
	obj.wr_1 = '';
	obj.wr_2 = '';
	obj.wr_3 = '';
	obj.wr_4 = '';
	obj.wr_5 = '';
	obj.wr_6 = '';
	obj.wr_7 = pLink;
	obj.wr_8 = arFileUrl0;
	obj.wr_9 = arFileUrl1;
	obj.wr_10 = arFileUrl2;
	obj.write_table 	= 'g4_write_'+sBo_table;
	obj.bo_table	 	= sBo_table;
	


	trace('--------------------------------------------------------');
	//trace(sContent);
	if(bDebug==true) {
		//return;
	}
	
	if(sSearch=="jjang") {
		hsSaveBrd_euckr.send(obj);	
	} else {
		hsSaveBrd.send(obj);
		
		if(sSearch!="jjang" && sSearch!="steel" && sSearch!="ygosu" && sSearch!="1004tv") {
			hsSaveBrdSfu.send(obj);	
		}
		
	}
	
	
	
}
public function onFalutView(e:Event):void
{
	Alert.show('onFalutView : ' + e.toString());
	
	trace('flaut:'+e.toString());
}


public var arTempMemberId:Array = new Array("ov5ver99", "gjwnsvq23tjr0203", "kdh1vq235377", "zldzkcvq235jfgud", "foxmavq235pia", "galbvq25i92", "bida64ls", "hugokv34won", "alsdlfv543rms", "jmsum8985", "startm89es20", "raynm89or1001", "duck90m98o44", "smslm89oa123", "corem89a5585", "chm9inja", "sks04m8905k", "ldym9way", "wndnn8js77", "lb8n7eka", "dlc3128266", "kyungc123ina", "soatc123paos4", "narc123rar", "sh12cout36", "sjgjc13728", "iiic2138288", "hsmdc1288", "baekdcjy88", "olnam1h1za55", "kimf31hmigor", "ann13fyo", "yookic13fo1226", "tmdvyf31di", "kdj031525", "leeof3hsun", "neoa31fssin", "nerf13o44", "ks3f1c334", "babi13e117", "wjdqhf1ddla", "qwqw11f24", "lovekd1ove87", "d14arkstom", "mars232979", "ehdzv32l45"); 

public var arTempMemberAlias:Array = new Array("아씨니", "guddldu", "kdj0525", "hahayaya", "annyo", "ssong", "박기평", "dqwdqd", "wef12", "enenRjdtm", "sks0405k", "건전지", "WINGCAR", "캐논히터c", "구하라가짱", "장예민", "부릉부릉", "짜리홍", "최재현", "foxmapia", "김동", "궛거", "asdgsg", "야왕진원", "남폭간", "멍미", "찰떡", "소퓨", "다비니", "qldtl", "형원이", "세균사랑", "강성훈", "신부라", "민석", "바보", "아르", "ybj1245", "광월의화염", "고가내", "다눔너", "왕눈이", "sinmin", "베러리충전", "냠냠냠이", "아아", "baxter", "고정민", "정석현", "킹카함");











